package com.apes.scm.voucher.service.ict;

import com.apes.framework.api.SpringManager;
import com.apes.scm.account.account.model.AccountingMethod;
import com.apes.scm.account.account.model.InventoryMode;
import com.apes.scm.account.account.service.InventoryService;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherItem;
import com.apes.scm.voucher.repository.VoucherInventoryRepository;
import com.apes.scm.voucher.service.BusinessType;
import com.apes.scm.voucher.service.VoucherService;
import com.apes.scm.voucher.service.VoucherType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.stream.Collectors;

import static java.lang.Math.abs;
import static java.util.stream.Collectors.groupingBy;

/**
 * 功能：采购订单公司间交易
 *
 * @author xul
 * @create 2018-10-18 9:26
 */
@Component
public class PurchaseOrderIct extends AbstractIct {

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private VoucherInventoryRepository voucherInventoryRepository;

    public PurchaseOrderIct() {
        this.BusinessTypes = new String[]{BusinessType.PURCHASE_ORDER};
    }

    /**
     * 公司间/跨公司交易：Inner-company/Cross-Company transactions
     *
     * @param voucher
     */
    @Override
    public void transaction(Voucher voucher) {
        voucherInventoryRepository.findByVoucher(voucher).stream()
                .filter(voucherInventory -> voucherInventory.getVoucherItem().cross()) //跨公司交易
                .collect(groupingBy(voucherInventory -> voucherInventory.getVoucherItem().obtainOwner()))
                .forEach((owner, vis) -> {
                    Voucher cv = new Voucher();
                    cv.setCompany(voucher.getCompany());//公司
                    cv.setSourceDept(voucher.getDept());//部门
                    cv.setPartner(getCustomer(owner));//合作伙伴
                    cv.setScene(voucher.getScene());
                    cv.setOriginBusinessType(voucher.getOriginBusinessType());
                    cv.setOrigin(voucher.getOrigin());
                    cv.setSourceBusinessType(voucher.getInnerBusinessType());
                    cv.setSource(voucher.getInnerBusinessKey());
                    cv.setInvoiced(true);
                    cv.setReturned(false);
                    cv.setAccountingMethod(AccountingMethod.AM_PURCHASE);//核算方式
                    cv.setInventoryMode(InventoryMode.IM_OWN);//库存方式
                    cv.setVoucherType(VoucherType.CROSS_TRANSFER);//凭证类型
                    cv.setBusinessType(BusinessType.TRANSFER_ORDER);
                    cv.setCustomerExpand(voucher.getCustomerExpand());


                    cv.setVoucherItems(vis.stream().map(vi -> {
                        /**冻结批次**/
                        inventoryService.changeFrostQty(vi.getInventoryBatch(), vi.getUom(), abs(vi.getProductQty()), voucher.getCompany(),
                                voucher.getInnerBusinessType(), voucher.getId(), String.valueOf(vi.getVoucherItem().getId()), "跨公司采购冻结批次");
                        VoucherItem voucherItem = new VoucherItem();
                        voucherItem.setProduct(vi.getProduct());//商品
                        voucherItem.setInventoryBatch(vi.getInventoryBatch());//价值批次
                        voucherItem.setSupplier(vi.getInventorySupplier());//供应商
                        voucherItem.setUom(vi.getUom());//计量单位
                        voucherItem.setPriceUnit(vi.getTaxCostPrice());//含税单价
                        voucherItem.setProductQty(vi.getProductQty());//数量
                        voucherItem.setOriginItemId(vi.getVoucherItem().getOriginItemId());//原始单据明细行
                        voucherItem.setSourceItemId(String.valueOf(vi.getVoucherItem().getId()));//源单据明细行
                        voucherItem.setVoucher(cv);
                        return voucherItem;
                    }).collect(Collectors.toSet()));
                    SpringManager.getBean(VoucherService.class).save(cv);
                });
    }
}
